<?php

declare(strict_types=1);

namespace App\Http\Controllers\Manage;

use App\Models\Supplier;
use App\Repositories\Manage\ShopRepository;
use App\Repositories\Manage\StallRepository;
use App\Repositories\Manage\StatRepository;
use App\Repositories\Manage\SupplierRepository;
use App\Repositories\ParkRepository;
use App\Repositories\PictureMappingRepository;

class SupplierController extends ManageBaseController
{

    private $supplierRepo = NULL;

    public function __construct(
        SupplierRepository $supplierRepo
    )
    {
        parent::__construct();
        $this->supplierRepo = $supplierRepo;

    }

    public function index()
    {
        $filter = request()->all();


        $res = $this->supplierRepo->getList($filter, $this->pageNum, $this->pageSize);

        return retSuc($res);
    }

    public function update($id = null)
    {
        $data = request()->only(['id', 'name', 'open_printer_task', 'type', 'stock_cal_type', 'title', 'is_open', 'park_id', 'supplier_id', 'app_name', 'order_end_time', 'mch_id', 'mch_secret', 'distrute_rank',
            'service_money', 'is_label', 'deliver_type', 'prepare_time', 'deliver_time', 'ava_price', 'contact_name', 'contact_phone', 'accept_order_type', 'is_remark', 'address', 'bag_price', 'ava_price', 'min_money', 'thumb', 'banner', 'description', 'store_time', 'location',
            'food_license', 'delivery_phone', 'business_license', 'address', 'status', 'store_time', 'sort', 'base_shipping_price', 'notice']);
        $pictureIds = request()->post("picture_ids");
        $role_id = request()->post("role_id");
        $step = request()->post("step", 0);

        //判断是否设置权限
        if ($step == 0 && empty($role_id)) {
            return retFail("请选择访问权限");
        }

        if ($step == 0) {
            $supplier_id = Supplier::query()->where(["title" => $data['title'], 'supplier_id' => $data['supplier_id']])->value("id");
            /* if ($supplier_id != $data['id']) {
                 return retFail("店铺名称已存在");
             }*/
        }

        $last_id = $this->supplierRepo->saveOrUpdate($data, $id ?? 0);
        if (!$last_id) {
            return retFail(empty($data['id']) ? "新增失败" : "更新失败");
        }

        if ($step == 0) {
            $this->supplierRepo->updateRole(intval($last_id), $role_id);
        }

        //店铺环境图
        if ($step == 3) {
            (new PictureMappingRepository())->updateMapping($last_id, "shop", $pictureIds ?? []);
        }

        $res = $this->supplierRepo->getOne($last_id);
        // 单店模式创建对应的档口
        if ($res['type'] == 0) {
            $stall = (new StallRepository())->getByShopId($last_id);
            $sData = [];
            $sData['shop_id'] = $last_id;
            $sData['status'] = 1;
            $sData['title'] = $res['title'] ?? "";
            (new StallRepository())->saveOrUpdate($sData, $stall['id'], 1);
        }
        return retSuc($res);
    }

    public function options()
    {
        try {
            $type = request()->get("type");

            $condition = [];
            if (!checkIsSuper(UID)) {
                $condition['shop_id'] = intval(getUserRelateShopId(UID));
            }
            $res = $this->supplierRepo->getOptions($type, $condition);
            return retSuc($res);
        } catch (\Exception $e) {
            return retFail($e->getMessage());
        }
    }

    /**
     * 编辑初始化数据
     */
    public function init()
    {
        $parks = (new ParkRepository())->getOptions();
        $shops = (new ShopRepository())->getOptions();//商户
        return retSuc(compact('parks', 'shops'));
    }

    /**
     * 统计数据
     */
    public function stat()
    {
        return (new StatRepository())->getMonthStat();
    }

    public function detail($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");
        $res = $this->supplierRepo->getOne($id);
        return retSuc($res);
    }

    public function delete($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return retFail("参数错误");
        }

        return $this->supplierRepo->deleteSupplier($id);
    }

}
